#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "minIni.h"
#include "config.h"
#include "resources.h"
#include "map.h"
#include "spriteanimator.h"
#include "sprite.h"
#include "blitqueue.h"
#include "grcontext.h"
#include "entityaction.h"
#include "entity.h"
#include "entitymanager.h"
#include "inputhandler.h"
#include "controller.h"
#include "player.h"
#include "game.h"

using namespace Oribi;

Game::Game(){}

Game::Game(int nPlayers)
{
    nPlayers++; //Account for gaia
    for (int i = 0; i < nPlayers; ++i)
    {
        players.push_back(new Player);
    }
}

Game::~Game()
{
    for (int i = 0; i < (int)players.size(); ++i)
    {
        delete players[i];
    }
}

Map& Game::getMap()
{
    return map;
}

Player *Game::getPlayer(int n)
{
    if (n >= 0 || n < (int)players.size())
        return players[n];
    else
        return NULL;
}

void Game::update()
{
    for (unsigned i = 0; i < players.size(); ++i)
    {
        players[i]->update(this);
    }
}

void Game::drawEverything(GraphicContext& gr)
{
    map.render(gr);
    int m = players.size();

    for (int i = 0; i < m; ++i)
    {
        players[i]->drawEverything(gr);
    }
}

int Game::initialise(ConfContext& conf, Resources& res)
{
    if (!map.initSprites(res))
        return 1;
    return 0;
}

void Game::createMap(int size)
{
    map.initialise(size);
}
